﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FreeProperty.Views.Shared;
using FreeProperty.Data;
using System.Data.SqlTypes;

namespace FreeProperty.Views.User
{
    public partial class UserProperty : SiteBasePageWithSession
    {
        string status = "0";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                SetUI();
            }
            else
            {
                SetPostbackVariables();
                switch (hdnMode.Value.ToUpper())
                {
                    case "SAVEUSER":
                        SaveUser();
                        break;
                    case "CHANGESTATUS":
                        ChangePropertyStatus();
                        break;
                    case "SEARCH":
                        hdnCurrentPage.Value = "1";
                        BindPagingData();
                        break;
                    case "SEARCHDATA":
                        BindPagingData();
                        break;

                }
            }
        }

        private void SetPostbackVariables()
        {
            if (!String.IsNullOrEmpty(Request.QueryString["inactive"]))
            {
                status = Request.QueryString["inactive"];
            }
            else
            {
                status = "0";
            }
            lblError.Text = "";
            lblResult.Text = "";
        }
        private void SaveUser()
        {
            UserInfo oUserInfo = new UserInfo();
            oUserInfo.ID = new SqlGuid(ActiveUser.ID.ToString());
            oUserInfo.EmailID = tbEmailId.Value.Trim();
            oUserInfo.Password = tbPassword.Value.Trim();
            oUserInfo.Name = tbName.Value.Trim();
            oUserInfo.ContactNo = tbPhoneNo.Value.Trim();
            SqlInt32 iError = oUserInfo.Save();
            if (iError > 0)
            {
                lblError.Text = oUserInfo.GetErrorMessage(iError);
            }
            else
            {
                lblError.Text = "User saved successfully.";
            }
        }
        private void ValidateUser()
        {
            UserInfo oUserInfo = new UserInfo();
            oUserInfo.Password = tbPassword.Value.Trim();
            UserInfo oLoggedInUserInfo = oUserInfo.ValidateLogin();
            if (oUserInfo.ID.IsNull)
            {
                lblError.Text = oLoggedInUserInfo.LoginError;
            }
            else
            {

                Session[FreePropertyConstants.SESSION_ACTIVE_USER] = oUserInfo;
                if (null == Session[FreePropertyConstants.SESSION_LOGIN_URL])
                {
                    lblError.Text = "Login Successfull.";
                }
                else
                {
                    Server.Transfer(Session[FreePropertyConstants.SESSION_LOGIN_URL].ToString());
                }
            }
        }

        private void ChangePropertyStatus()
        {
            if (!String.IsNullOrEmpty(Request.QueryString["id"]) && !String.IsNullOrEmpty(status))
            {
                PropertyDesc oPropertyDesc = new PropertyDesc();
                oPropertyDesc.ID = new SqlGuid(Request.QueryString["id"]);
                oPropertyDesc.Inactive = status == "0" ? true : false;
                oPropertyDesc.ChangePropertyStatus();
                hdnCurrentPage.Value = "1";
                BindPagingData();
            }
        }
        private void SetUI()
        {
            hdnUserID.ID = ActiveUser.ID.ToString();
            lblEmailId.Text = tbEmailId.Value = ActiveUser.EmailID;
            lblName.Text = tbName.Value = ActiveUser.Name;
            lblPassword.Text = tbPassword.Value = ActiveUser.Password;
            lblPhoneNo.Text = tbPhoneNo.Value = ActiveUser.ContactNo;
            status = "0";
            hdnCurrentPage.Value = "1";
            BindPagingData();
        }

        private void InitPaging()
        {
            if (hdnStatus.Value.Length > 0)
            {
                switch (hdnStatus.Value.ToUpper())
                {

                    case "PAGING":
                        break;
                    case "PREVIOUS":
                        hdnCurrentPage.Value = (Convert.ToInt32(hdnCurrentPage.Value) - 1).ToString();
                        break;
                    case "NEXT":
                        hdnCurrentPage.Value = (Convert.ToInt32(hdnCurrentPage.Value) + 1).ToString();
                        break;
                    case "FIRST":
                        hdnCurrentPage.Value = "1";
                        break;
                    case "LAST":
                        hdnCurrentPage.Value = hdnPageCount.Value;
                        break;
                }
                //BindPagingData();
            }
        }
        protected void BindPagingData()
        {
            InitPaging();

            PagerData oPagerData = new PagerData { PageSize = PagingUtil.PageSize, PageNumber = Convert.ToInt32(hdnCurrentPage.Value) };
            PropertyDesc oPropertyDesc = GetSearchDescription();
            List<PropertyDesc> lst = oPropertyDesc.LoadUserProperties(ActiveUser.ID, oPagerData);
            divPageNoFooter.Visible = lst.Count > 0 ? true : false;
            if (lst.Count == 0)
            {
                lblResult.Text = " No record found.";
                lblResult.Visible = true;
            }
            hdnPageCount.Value = oPagerData.TotalPages.ToString();

            PagingUtil.BindDataToGrid(grdSearchResults, lst, "Views/User/UserProperty.aspx?inactive=" + status, "AD", spanFootServerPaging, hdnCurrentPage, hdnPageCount, lnkFootNext, lnkFootLast, lnkFootFirst, lnkFootPrevious);
        }
        protected void grdSearchResults_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                PropertyDesc oPropertyDesc = (PropertyDesc)e.Row.DataItem;
                Button btnChangeStatus = (Button)e.Row.FindControl("btnChangeStatus");
                Button btnEdit = (Button)e.Row.FindControl("btnEdit");
                string url = "return ChangePropertyStatus('Views/User/UserProperty.aspx?inactive=" + status + "&id=" + oPropertyDesc.ID + "')";
                btnChangeStatus.Attributes.Add("onclick", url);
                btnChangeStatus.Text = status == "0" ? "Mark Inactive" : "Mark Active";

                if (status != "0")
                {
                    btnEdit.Visible = false;
                }
                else
                {
                    string editUrl = "javascript:return AjaxGET('Views/Main/PostProperty.aspx?editid="+oPropertyDesc.ID+"',OnSuccess,OnError)";
                    btnEdit.Attributes.Add("onclick", editUrl);
                }
            }

        }
        private PropertyDesc GetSearchDescription()
        {
            PropertyDesc oPropertyDesc = new PropertyDesc();

            oPropertyDesc.Inactive = status == "0" ? false : true;
            return oPropertyDesc;
        }
    }
}
